package com.mdxx.xczd.utils;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.media.MediaMetadataRetriever;

import java.io.File;
import java.io.FileOutputStream;

public class VideoUtil {

	@SuppressLint("NewApi") public static String[] getBitmapsFromVideo(String video_url) { 
		
		MediaMetadataRetriever retriever = new MediaMetadataRetriever(); 
		retriever.setDataSource(video_url); 
		String time = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION); 
		int seconds = Integer.valueOf(time) / 1000; 
		String[] paths = new String[seconds];
		for (int i = 1; i <= seconds; i++) { 
			Bitmap bitmap = retriever.getFrameAtTime(i*1000*1000,MediaMetadataRetriever.OPTION_CLOSEST_SYNC); 
			String path = video_url + i + ".jpg"; 
			paths[i-1] = path;
			FileOutputStream fos = null; 
			try { 
				fos = new FileOutputStream(new File(path)); 
				bitmap.compress(CompressFormat.JPEG, 40, fos); 
				fos.close(); 
			} catch (Exception e) { 
				e.printStackTrace(); 
			} 
		} 
		return paths;
	} 
}
